package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.Result;
import com.youlai.system.mapper.StockProducterMapper;
import com.youlai.system.model.entity.Stock;
import com.youlai.system.model.entity.StockProducter;
import com.youlai.system.model.entity.Stockmanufactor;
import com.youlai.system.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @作业 chen
 * @时间 2024-08-28 11:49
 */

@RestController
@RequestMapping("/api/v1/StockProvider")
@RequiredArgsConstructor
@Slf4j
public class StockProducterController {
    private final StockProducterMapper stockProducterMapper;
    @PostMapping("/selectStock")
    public Result selectStock(@RequestBody Stock stock) {
        Page page = new Page<>(stock.getCurrentPage4(), stock.getPageSize4());
        LambdaQueryWrapper<StockProducter> stockLambdaQueryWrapper = new LambdaQueryWrapper<>();
        stockLambdaQueryWrapper.eq(!stock.getName().isEmpty(),StockProducter::getProducterName,stock.getName());
        stockLambdaQueryWrapper.like(!stock.getKeywords().isEmpty(),StockProducter::getKeywords,stock.getKeywords());
        stockLambdaQueryWrapper.eq(!stock.getIphone().isEmpty(),StockProducter::getProducterTel,stock.getIphone());
        stockLambdaQueryWrapper.like(!stock.getTime().isEmpty(),StockProducter::getCreateTime,stock.getTime());
        System.out.println(stock.getTime());
//        page.setRecords(stockProducterMapper.selectPage(page, stockLambdaQueryWrapper).getRecords());
        Page page1 = stockProducterMapper.selectPage(page, stockLambdaQueryWrapper);
        return Result.success(page1);
    }

    //根据id查询的方法
    @PostMapping("/updateselectStock/{producterId}")
    public Result updateselectStock(@PathVariable("producterId") String producterId) {
//        StockProducter stockProducter = stockProducterMapper.selectById(producterId);
        LambdaQueryWrapper<StockProducter> stockLambdaQueryWrapper = new LambdaQueryWrapper<>();
        stockLambdaQueryWrapper.eq(StockProducter::getProducterId,producterId);
        StockProducter stockProducter = stockProducterMapper.selectOne(stockLambdaQueryWrapper);
        System.out.println(stockProducter);
//        page.setRecords(stockProducterMapper.selectPage(page, stockLambdaQueryWrapper).getRecords());
        return Result.success(stockProducter);
    }

    @PostMapping("/addStock")
    @Transactional
    public Result addStock(@RequestBody Stockmanufactor Stockmanufactor) {
        LambdaQueryWrapper<StockProducter> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StockProducter::getProducterName, Stockmanufactor.getName());
        queryWrapper.eq(StockProducter::getProducterCode, Stockmanufactor.getCode());
        queryWrapper.eq(StockProducter::getProducterPerson, Stockmanufactor.getContacts());
        queryWrapper.eq(StockProducter::getProducterAddress, Stockmanufactor.getAddress());
        queryWrapper.eq(StockProducter::getProducterTel, Stockmanufactor.getIphone());
        queryWrapper.eq(StockProducter::getKeywords, Stockmanufactor.getKeyword());
        queryWrapper.eq(StockProducter::getStatus, Stockmanufactor.getStatus());
        //获取当前时间
        queryWrapper.eq(StockProducter::getCreateTime,new Date());
        queryWrapper.eq(StockProducter::getCreateBy, SecurityUtils.getUsername());
//        System.out.println(queryWrapper.getSqlSelect());
       //添加方法

        if (stockProducterMapper.selectOne(queryWrapper) == null) {
            StockProducter stockProducter = new StockProducter();
            stockProducter.setProducterName(Stockmanufactor.getName());
            stockProducter.setProducterCode(Stockmanufactor.getCode());
            stockProducter.setProducterPerson(Stockmanufactor.getContacts());
            stockProducter.setProducterAddress(Stockmanufactor.getAddress());
            stockProducter.setProducterTel(Stockmanufactor.getIphone());
            stockProducter.setKeywords(Stockmanufactor.getKeyword());
            stockProducter.setStatus(String.valueOf(Stockmanufactor.getStatus()));
            stockProducter.setCreateBy(SecurityUtils.getUsername());
            stockProducter.setCreateTime(new Date());
            stockProducterMapper.insert(stockProducter);
            return Result.success();
        } else {
            return Result.failed("该供应商信息已存在！");
        }
    }

    //删除方法
    @PostMapping("/deleteStock/{producterId}")
    @Transactional
    public Result deleteStock(@PathVariable("producterId") String producterId) {
//        System.out.println(producterId);
        LambdaQueryWrapper<StockProducter> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StockProducter::getProducterId,producterId);
        //删除方法
        stockProducterMapper.delete(queryWrapper);
        return Result.success();
    }


    //修改方法
    @PostMapping("/updateStock")
    @Transactional
    public Result updateStock(@RequestBody Stockmanufactor Stockmanufactor) {
        LambdaUpdateWrapper<StockProducter> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(StockProducter::getProducterId, Stockmanufactor.getId());
        updateWrapper.set(StockProducter::getProducterName, Stockmanufactor.getName());
        updateWrapper.set(StockProducter::getProducterCode, Stockmanufactor.getCode());
        updateWrapper.set(StockProducter::getProducterPerson, Stockmanufactor.getContacts());
        updateWrapper.set(StockProducter::getProducterAddress, Stockmanufactor.getAddress());
        updateWrapper.set(StockProducter::getProducterTel, Stockmanufactor.getIphone());
        updateWrapper.set(StockProducter::getKeywords, Stockmanufactor.getKeyword());
        updateWrapper.set(StockProducter::getStatus, String.valueOf(Stockmanufactor.getStatus()));
        updateWrapper.set(StockProducter::getUpdateBy, SecurityUtils.getUsername());
        updateWrapper.set(StockProducter::getUpdateTime, new Date());
        //修改方法
        stockProducterMapper.update(null, updateWrapper);
        return Result.success();
    }



}
